package com.bmps.springcloud.insuredmicroservice.util.trial;

import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.model.trail.TrialPersonModel;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lyw
 * @date 2019/07/24
 */
public class TrialUtil {

    private static final int TRIAL_INDEX_ZERO = 0;

    private static final int TRIAL_INDEX_ONE = 1;

    private static final int TRIAL_INDEX_TWO = 2;

    private static final int TRIAL_INDEX_THREE = 3;

    private static final String TRIAL_NULL = "null";

    public static String formatScaleNumber(String format,double value) {
        DecimalFormat decimalFormat = new DecimalFormat(format);
        return decimalFormat.format(value);
    }

    public static String roundHalfUp(double value) {
        BigDecimal bigDecimal = new BigDecimal(String.valueOf(value)).setScale(2,BigDecimal.ROUND_HALF_UP);
        return String.valueOf(bigDecimal.doubleValue());
    }
    
    public static BigDecimal bigDecimalRoundHalfUp(double value){
        return new BigDecimal(String.valueOf(value)).setScale(2,BigDecimal.ROUND_HALF_UP);
    }

  
    public static String getRiskRateKey(TrialPersonModel trialPersonModel, String order) {
        String[] split = order.split("\\|");
        StringBuilder sb = new StringBuilder();
        sb.append(AcceptConstant.REDIS_PREFIX_R003).append("|")
                .append(trialPersonModel.getRiskCode())
                .append("|G");
        Field[] fields = trialPersonModel.getClass().getDeclaredFields();
        try {
            for (String value : split) {
                for (Field field : fields) {
                    if (StringUtils.equalsIgnoreCase(value, field.getName())) {
                        field.setAccessible(true);
                        sb.append("|").append(field.get(trialPersonModel));
                        break;
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static String getRateKey(TrialPersonModel trialPersonModel, String order,String type) {
        String[] split = order.split("\\|");
        StringBuilder sb = new StringBuilder();
        sb.append(AcceptConstant.REDIS_PREFIX_R003).append("|")
                .append(trialPersonModel.getRiskCode())
                .append("|")
                .append(type);
        Field[] fields = trialPersonModel.getClass().getDeclaredFields();
        try {
            for (String value : split) {
                for (Field field : fields) {
                    if (StringUtils.equalsIgnoreCase(value, field.getName())) {
                        field.setAccessible(true);
                        sb.append("|").append(field.get(trialPersonModel));
                        break;
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void setElementMapping(TrialPersonModel trialPersonModel, String mapping) {
        //费率->HCT014:{a=Get, b=VPU, c=Rate, d=PayIntvRate}
        mapping = mapping.replace("{", "");
        mapping = mapping.replace("}", "");
        String[] param = mapping.split(",");
        Map<String, String> result = new HashMap<>(param.length);
        for (String s : param) {
            int index = s.indexOf('=');
            result.put(s.substring(0, index).trim(), s.substring((index + 1)));
        }
        Field[] fields = trialPersonModel.getClass().getDeclaredFields();
        try {
            for (Map.Entry<String, String> entry : result.entrySet()) {
                for (Field field : fields) {
                    if (StringUtils.equalsIgnoreCase(entry.getValue(), field.getName())) {
                        field.setAccessible(true);
                        Object value = field.get(trialPersonModel);
                        if (value == null) {
                            result.put(entry.getKey(), null);
                        } else {
                            result.put(entry.getKey(), value.toString());
                        }
                        break;
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        trialPersonModel.setMap(result);
    }

    public static void setSpecialElement(TrialPersonModel trialPersonModel, String result) {
        if (result != null && !StringUtils.equals(result, TRIAL_NULL)) {
            String[] str = result.split("\\|");
            trialPersonModel.setVpu(str[TRIAL_INDEX_ZERO]);
            trialPersonModel.setPayEndYearRela(str[TRIAL_INDEX_ONE]);
            if (!StringUtils.isEmpty(str[TRIAL_INDEX_TWO])) {
                trialPersonModel.setPayEndYear(Integer.valueOf(str[TRIAL_INDEX_TWO]));
            }
            trialPersonModel.setPayEndYearFlag(str[TRIAL_INDEX_THREE]);
        }
    }

    /**
     * 设置费率
     */
    public static void setElementRate(TrialPersonModel trialPersonModel, String rate) {
        if (rate == null || TRIAL_NULL.equals(rate)) {
            trialPersonModel.setRate(null);
        } else {
            trialPersonModel.setRate(Double.valueOf(rate));
        }
    }


}
